package com.sinopac.action.LogQuery;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.model.Workbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;


//import com.itextpdf.text.pdf.codec.Base64.InputStream;
import com.sinopac.actionCore.ActionCore;
import com.sinopac.entity.LogQuery.LogVO;
import com.sinopac.entity.LogQuery.LoginLogVO;
import com.sinopac.entity.LogQuery.VerifyLogVO;
import com.sinopac.model.LogQuery.LogQueryModel;

@Namespace("/LogQuery")
@ParentPackage("struts-default")
public class LogQueryAction extends ActionCore{
	private static final long serialVersionUID = 1L;
	private List<LogVO> logList;
	private List<VerifyLogVO> verifyLogList;
	private List<LoginLogVO> loginLogList;
	private String record_str;
	private Workbook wb;
	private InputStream fileInputStream;
    private String fileName = "record.xls";

//	private HttpServletRequest request; 
//	private HttpServletResponse response;
//	private static final String LOGIN = "function1";

	@Action(value="index",results={@Result(name="input",location="/form/LogQuery/tokenRecord.jsp"),
								   @Result(name="success",location="/form/LogQuery/checkRecord.jsp"),
								   @Result(name="login",location="/form/LogQuery/loginRecord.jsp")})
	public String index(){
		System.out.println("to_h"+(String)session.get("to_h"));
		LogQueryModel lqm = new LogQueryModel();
		record_str = (String)session.get("record");
		if("異動紀錄".equals((String)session.get("record"))||"申請紀錄".equals((String)session.get("record"))||
		   "註銷紀錄".equals((String)session.get("record"))){
			logList = lqm.getTokenRecord((String)session.get("from_h"),(String)session.get("to_h"),(String)session.get("token_cust"),(String)session.get("record"));
		}else if("登入紀錄".equals((String)session.get("record"))){
			loginLogList = lqm.getLoginRecord((String)session.get("from_h"),(String)session.get("to_h"));
			return LOGIN;
		}else{
			verifyLogList = lqm.getCheckRecord((String)session.get("from_h"),(String)session.get("to_h"));
			return SUCCESS;
		}
		return INPUT;
	}
	
//	@Action(value="exportExcel",results={@Result(name="input",location="/form/LogQuery/tokenRecord.jsp"),
//			   							 @Result(name="success",location="/form/LogQuery/checkRecord.jsp"),
//			   							 @Result(name="login",location="/form/LogQuery/loginRecord.jsp")})
	@Action(value="exportExcel", results={@Result(name="success", type="stream", 
			params = {"contentType", "application/vnd.ms-excel",
					  "inputName", "fileInputStream",
					  "contentDisposition", "filename=\"${fileName}\"",
					  "bufferSize", "1024"})})
	public String exportExcel(){
		String filePath = "";
		LogQueryModel lqm = new LogQueryModel();
		if("異動紀錄".equals((String)session.get("record"))||"申請紀錄".equals((String)session.get("record"))||
		   "註銷紀錄".equals((String)session.get("record"))){
			logList = lqm.getTokenRecord((String)session.get("from_h"),(String)session.get("to_h"),(String)session.get("token_cust"),(String)session.get("record"));
			try {
				filePath = lqm.exportLog(logList, (String)session.get("record"));
				fileInputStream = new FileInputStream(new File(filePath));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("登入紀錄".equals((String)session.get("record"))){
			loginLogList = lqm.getLoginRecord((String)session.get("from_h"),(String)session.get("to_h"));
			try {
				filePath = lqm.exportLogin(loginLogList, (String)session.get("record"));
				fileInputStream = new FileInputStream(new File(filePath));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			verifyLogList = lqm.getCheckRecord((String)session.get("from_h"),(String)session.get("to_h"));
			try {
				filePath = lqm.exportVerify(verifyLogList, (String)session.get("record"));
				fileInputStream = new FileInputStream(new File(filePath));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
//		System.out.println("logList=" + logList.get(0).getCustomer_id());
		return SUCCESS;
	}
	
	public List<LogVO> getLogList() {
		return logList;
	}

	public void setLogList(List<LogVO> logList) {
		this.logList = logList;
	}

	public List<VerifyLogVO> getVerifyLogList() {
		return verifyLogList;
	}

	public void setVerifyLogList(List<VerifyLogVO> verifyLogList) {
		this.verifyLogList = verifyLogList;
	}

	public List<LoginLogVO> getLoginLogList() {
		return loginLogList;
	}

	public void setLoginLogList(List<LoginLogVO> loginLogList) {
		this.loginLogList = loginLogList;
	}

	public String getRecord_str() {
		return record_str;
	}

	public void setRecord_str(String record_str) {
		this.record_str = record_str;
	}

	public InputStream getFileInputStream() {
		return fileInputStream;
	}

	public void setFileInputStream(InputStream fileInputStream) {
		this.fileInputStream = fileInputStream;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
//	System.out.println("record=" + record);
}
